package com.briup.result;

import lombok.Getter;

/**
 * Project: jd2407_third
 * Package: com.briup.util
 * Author: XBai
 * Time: 2024/10/29 14:44
 */

@Getter
public class Result<D> {
    private Integer code;
    private String msg;
    private D data;
    private Long timestamp; //时间戳

    {
        timestamp = System.currentTimeMillis();
    }

    //构造器私有化 外界不允许创建对象
    private Result() {
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private Result(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
        this.timestamp = System.currentTimeMillis();
    }

    //1.成功、不带数据 增删改
    public static Result success() {
        return success(null);
    }

    //2.成功、带数据 查询
    public static <D> Result<D> success(D data) {
        Result<D> result = new Result<>(ResultCode.SUCCESS);
        result.data = data;
        return result;
    }

    //3.失败，不带数据
    public static Result failure(ResultCode resultCode) {
        return new Result(resultCode);
    }


    public static Result failure(Integer code, String msg) {
        return new Result(code,msg);
    }
}
